package com.first.system.service.impl;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.first.common.pojo.CrmBanner;
import com.first.common.pojo.TbHeadNav;
import com.first.common.vo.Result;
import com.first.system.dao.BannerDao;
import com.first.system.service.BannerService;
import com.first.system.vo.BannerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @program: first-project
 * @description:
 * @author: 闫锦超
 * @create: 2023-05-26 16:59
 **/
@Service
public class BannerServiceImpl implements BannerService {
    @Autowired
    private BannerDao bannerDao;
    @Override
    public Result getAllBanner(Integer currentPage, Integer pageSize, BannerVo bannerVo) {
        Page<CrmBanner> page = new Page(currentPage,pageSize);
        QueryWrapper<CrmBanner> wrapper = new QueryWrapper<>();
        wrapper.eq("is_deleted",0);
        if (StringUtils.hasText(bannerVo.getTitle())){
            wrapper.like("title",bannerVo.getTitle());
        }
        if(bannerVo.getIsDisable()!=""&& bannerVo.getIsDisable()!=null){
            wrapper.eq("is_disable",bannerVo.getIsDisable());
        }
        Page<CrmBanner> tbHeadNavPage = bannerDao.selectPage(page, wrapper);
        return Result.success(tbHeadNavPage);
    }

    @Override
    public Result changeStatus(String id, String isDisable) {
        CrmBanner crmBanner = new CrmBanner();
        crmBanner.setId(id);
        crmBanner.setIsDisable(isDisable);
        int i = bannerDao.updateById(crmBanner);
        if(i!=0){
            return Result.success();
        }
        return null;
    }

    @Override
    public Result addBanner(CrmBanner crmBanner) {
        if(StringUtils.hasText(crmBanner.getId())){
            bannerDao.updateById(crmBanner);
            return Result.success();
        }
        crmBanner.setId(IdUtil.getSnowflakeNextId()+"");
        int insert = bannerDao.insert(crmBanner);
        if(insert!=0){
            return Result.success();
        }
        return null;
    }

    @Override
    public Result deleteBanner(CrmBanner crmBanner) {
        crmBanner.setIsDeleted("1");
        int i = bannerDao.updateById(crmBanner);
        if(i!=0){
            return Result.success();
        }
        return null;
    }
}
